﻿using LightContracts;
using NUnit.Framework;

namespace LightContractsTests
{
    [TestFixture]
    public class RequiresTest
    {
        [Test]
        public void RequireTrue()
        {
            Requires.That(true,"desc");
        }

        [Test]
        public void RequireIsNotNullTrue()
        {
            Requires.IsNotNull(new object(), "name");
        }

        [Test]
        public void RequireFalse()
        {
            try
            {
                Requires.That(false, "desc");
                Assert.Fail("This code should not be reached.");
            }
            catch(AssertException exception)
            {
                Assert.AreEqual("Precondition failed. The expectation was 'desc', but this is false.", exception.Message);
            }
        }

        [Test]
        public void RequireFalseWithNamedMethod()
        {
            try
            {
            	Requires.That(IsInit);
                Assert.Fail("This code should not be reached.");
            }
            catch(AssertException exception)
            {
                Assert.AreEqual("Precondition failed. The expectation was 'IsInit', but this is false.", exception.Message);
            }
        }

        private bool IsInit()
        {
        	return false;	
        }

        
        [Test]
        public void RequireFalseWithAnonymousMethod()
        {
            try
            {
            	Requires.That(delegate(){return false;});
                Assert.Fail("This code should not be reached.");
            }
            catch(AssertException exception)
            {
                Assert.AreEqual("Precondition failed. The expectation was 'anonymous function delivers true', but this is false.", exception.Message);
            }
        }

        [Test]
        public void RequireFalseWithLambdaExpression()
        {
            try
            {
            	Requires.That(()=> false);
                Assert.Fail("This code should not be reached.");
            }
            catch(AssertException exception)
            {
                Assert.AreEqual("Precondition failed. The expectation was 'anonymous function delivers true', but this is false.", exception.Message);
            }
        }

        
        [Test]
        public void RequireIsNotNullFalse()
        {
            try
            {
                Requires.IsNotNull(null, "name");
                Assert.Fail("This code should not be reached.");
            }
            catch (AssertException exception)
            {
                Assert.AreEqual("Precondition failed. The expectation was 'name is not null', but this is false.", exception.Message);
            }
        }
    }
}
